//
//  BLTopicTableViewController.m
//  百思不得姐
//
//  Created by hbl on 16/3/19.
//  Copyright © 2016年 hbl. All rights reserved.
//

#import "BLTopicTableViewController.h"
#import "BLNetwork.h"
#import "MJExtension.h"
#import "BLTopic.h"
#import "BLTopicTableViewCell.h"
#import "MJRefresh.h"
#import "BLCommentViewController.h"

@interface BLTopicTableViewController ()
@property (nonatomic, strong)NSMutableArray *dataArray;
@property (nonatomic, strong)NSString *maxTime;
@end

@implementation BLTopicTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTableView];
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    [self.tableView.mj_header beginRefreshing];
}

- (void)setupTableView
{
    self.tableView.contentInset = UIEdgeInsetsMake(99, 0, self.tabBarController.tabBar.frame.size.height , 0);
    self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([BLTopicTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"topic"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
}

- (void)getData
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"a"] = self.nearTopic;
    params[@"c"] = @"data";
    params[@"type"] = self.type;
    [BLNetwork GET:@"http://api.budejie.com/api/api_open.php" parameter:params success:^(id result) {
        self.dataArray = [BLTopic mj_objectArrayWithKeyValuesArray:result[@"list"]];
        self.maxTime = result[@"info"][@"maxtime"];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

- (void)loadMore
{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"a"] = self.nearTopic;
    params[@"c"] = @"data";
    params[@"type"] = self.type;
    params[@"maxtime"] = self.maxTime;
    
    [BLNetwork GET:@"http://api.budejie.com/api/api_open.php" parameter:params success:^(id result) {
       
        self.maxTime = result[@"info"][@"maxtime"];
        NSArray *newData = [BLTopic mj_objectArrayWithKeyValuesArray:result[@"list"]];
        [self.dataArray addObjectsFromArray:newData];
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
    }];
    
}


#pragma mark - talbleviewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BLTopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"topic"];
    cell.topic = self.dataArray[indexPath.row];
    return  cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BLTopic *topic = self.dataArray[indexPath.row];
    return topic.cellHight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    BLCommentViewController *commentVc = [[BLCommentViewController alloc]init];
    commentVc.topic = self.dataArray[indexPath.row];
    [self.navigationController pushViewController:commentVc animated:YES];
}
@end
